import { Compiler, Component, ElementRef, OnInit, Input, ViewChild, AfterViewInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../server/server';

import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { Pagination } from '../../public/serve/pcrudInfo';
import { RoleTableComponent } from '../role/role-table/role-table.component';
import { RoleRolebyComponent } from '../../user/role/role-roleby/role-roleby.component';
import { defaultSearchType } from '../../public/serve/pcrudInfo';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
declare var $: any;

interface Response {
  result: string[];
  result_code: string;
  message: string;
  length: number;
}

export class rolesDatatype {
  public createDate: string[];
  public createUserId: string;
  public description: string;
  public lastUpdatedDate: string;
  public lastUpdatedUserId: string;
  public roleGroupId: string;
  public roleGroupName: string;
}


@Component({
  selector: 'app-role',
  templateUrl: './role.component.html',
})

export class RoleComponent implements OnInit {

  tableNumber: number = 0;

  right: any = {
    "user": {},
    "role": {}
  };
  constructor(private compiler: Compiler, private http: HttpClient, private server: Service, private router: Router, private removeTableService: RemoveTableService, private cudTableService: CudTableService) {
    this.right = $.extend(this.right, server.getRight());
  }

  public roleData: any[];
  public isActive = this.server.isActive;

  public isLoadTableFromPagination: any;

  ngOnInit() {
    this.searchData = {
      queryCriteria: '',
      queryType: 3,
      pageNumber: 1,
      pageSize: 20,
    };
    this.server.eventEmit.subscribe((value: any) => {
      this.isActive = value;
    });
  }

  ngDoCheck() {
    this.right = $.extend(this.right, this.server.getRight());
  }

  //导入table
  @ViewChild("roleTable")
  roleTable: RoleTableComponent;

  // 删除按钮
  isSelect: boolean = true;
  isSelectFromTableHandler(isSelect: boolean) {
    this.isSelect = isSelect;
  }

  // 禁用
  reduction() {
    setTimeout(() => {
      this.isSelect = true;
    }, 100);
  }

  // 表格
  public totalItems: number;
  ParentList(totalItems: number) {
    this.totalItems = totalItems;
  }

  //新增
  public newly_added_result_code: string;
  ParentAdd(result_code: string) {
    this.roleTable.getTabList(1, this.pageSize, '0', this.searchData.queryCriteria);
  }

  //详情
  public tabVal: any[];
  ParentDetails(tabVal: any[]) {
    this.tabVal = tabVal;
  }



  //删除click
  public deletes() {
    if (this.removeTableService.getRemoveTableId.length == 0) {
      this.server.prompt = { prompt: true, success: false, fail: false, promptStatement: "请选中要删除的数据!" }
      this.server.promptclose;
    } else {
      $("#delete").modal('show');
    }
  }
  //删除
  private remove_result_code: string;
  ParentDelete(result_code: string) {
    this.roleTable.getTabList(1, this.pageSize, '0', this.searchData.queryCriteria);
    this.roleTable.cancelSelectAll();
  }

  // 当前页（默认为第一页）
  private pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  private pageSize: number = 20;
  // id
  private id: string = '0';
  // 分页
  private pagination: Pagination;
  isLoadTableFromPaginationHandler(pagination: Pagination) {
    this.pagination = pagination;

    this.pageNumber = this.pagination[0].pageNumber;
    this.pageSize = this.pagination[0].pageSize;
    this.id = this.pagination[0].id;

    this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

    this.roleTable.getTabList(this.pageNumber, this.pageSize, this.id, this.searchData.queryCriteria);
  }

  public searchData: defaultSearchType;
  //查询
  search() {
    this.roleTable.getTabList(this.pageNumber, this.pageSize, this.id, this.searchData.queryCriteria);
  }
  //响应搜索
  changesearch() {
    this.search();
  }

}
// details
export class roleDetailsDatatype {
  public createDate?: string;
  public createUserId?: number;
  public description?: string;
  public lastUpdatedDate?: string;
  public roleGroupId?: string;
  public roleGroupName?: string;
  public astUpdatedUserId?: number;
  public roleName?: string;
  public roleId?: string;
  public autoAssignment?: number;
  public roleCode?: string;
  public parentRoleId?: string;
}
